<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webdoc</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            webdoc
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li class="active"><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-dva.html">30.cms-10-dva</a></li><li><a href="../html/30.cms-11-front.html">30.cms-11-front</a></li><li><a href="../html/31.cms-12-api.html">31.cms-12-api</a></li><li><a href="../html/31.cms-13-front.html">31.cms-13-front</a></li><li><a href="../html/31.cms-14-deploy.html">31.cms-14-deploy</a></li><li><a href="../html/32.ant.html">32.ant</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 什么是Buffer">1. 什么是Buffer</a></li><li><a href="#t12. 什么是字节">2. 什么是字节</a></li><li><a href="#t2进制">进制</a></li><li><a href="#t33. 定义buffer的三种方式">3. 定义buffer的三种方式</a><ul><li><a href="#t43.1 通过长度定义buffer">3.1 通过长度定义buffer</a></li><li><a href="#t53.2 通过数组定义buffer">3.2 通过数组定义buffer</a></li><li><a href="#t63.3 字符串创建">3.3 字符串创建</a></li></ul></li><li><a href="#t74.buffer常用方法">4.buffer常用方法</a><ul><li><a href="#t84.1 buf.fill(value[, offset[, end]][, encoding])">4.1 buf.fill(value[, offset[, end]][, encoding])</a></li><li><a href="#t94.2 write方法">4.2 write方法</a></li><li><a href="#t104.3 writeInt8">4.3 writeInt8</a><ul><li><a href="#t114.3.1 Little-Endian&Big-Endian">4.3.1 Little-Endian&Big-Endian</a></li></ul></li><li><a href="#t124.3 toString方法">4.3 toString方法</a></li><li><a href="#t134.4 slice方法">4.4 slice方法</a><ul><li><a href="#t144.4.1 截取乱码问题">4.4.1 截取乱码问题</a></li></ul></li><li><a href="#t154.5 copy方法">4.5 copy方法</a></li><li><a href="#t164.6 concat方法">4.6 concat方法</a></li><li><a href="#t174.7 isBuffer">4.7 isBuffer</a></li><li><a href="#t184.8 length">4.8 length</a></li></ul></li><li><a href="#t196. base64">6. base64</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x4EC0;&#x4E48;&#x662F;Buffer">1. &#x4EC0;&#x4E48;&#x662F;Buffer <a href="#t01. &#x4EC0;&#x4E48;&#x662F;Buffer"> # </a></h2>
<ul>
<li>&#x7F13;&#x51B2;&#x533A;Buffer&#x662F;&#x6682;&#x65F6;&#x5B58;&#x653E;&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x6570;&#x636E;&#x7684;&#x4E00;&#x6BB5;&#x5185;&#x5B58;&#x3002;</li>
<li>JS&#x8BED;&#x8A00;&#x6CA1;&#x6709;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x800C;&#x5728;&#x5904;&#x7406;TCP&#x548C;&#x6587;&#x4EF6;&#x6D41;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5FC5;&#x987B;&#x8981;&#x5904;&#x7406;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#x3002;</li>
<li>NodeJS&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;Buffer&#x5BF9;&#x8C61;&#x6765;&#x63D0;&#x4F9B;&#x5BF9;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#x7684;&#x64CD;&#x4F5C;</li>
<li>&#x662F;&#x4E00;&#x4E2A;&#x8868;&#x793A;&#x56FA;&#x5B9A;&#x5185;&#x5B58;&#x5206;&#x914D;&#x7684;&#x5168;&#x5C40;&#x5BF9;&#x8C61;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x8981;&#x653E;&#x5230;&#x7F13;&#x5B58;&#x533A;&#x4E2D;&#x7684;&#x5B57;&#x8282;&#x6570;&#x9700;&#x8981;&#x63D0;&#x524D;&#x786E;&#x5B9A;</li>
<li>Buffer&#x597D;&#x6BD4;&#x7531;&#x4E00;&#x4E2A;8&#x4F4D;&#x5B57;&#x8282;&#x5143;&#x7D20;&#x7EC4;&#x6210;&#x7684;&#x6570;&#x7EC4;&#xFF0C;&#x53EF;&#x4EE5;&#x6709;&#x6548;&#x7684;&#x5728;JavasScript&#x4E2D;&#x8868;&#x793A;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;</li>
</ul>
<h2 id="t12. &#x4EC0;&#x4E48;&#x662F;&#x5B57;&#x8282;">2. &#x4EC0;&#x4E48;&#x662F;&#x5B57;&#x8282; <a href="#t12. &#x4EC0;&#x4E48;&#x662F;&#x5B57;&#x8282;"> # </a></h2>
<ul>
<li>&#x5B57;&#x8282;(Byte)&#x662F;&#x8BA1;&#x7B97;&#x673A;&#x5B58;&#x50A8;&#x65F6;&#x7684;&#x4E00;&#x79CD;&#x8BA1;&#x91CF;&#x5355;&#x4F4D;&#xFF0C;&#x4E00;&#x4E2A;&#x5B57;&#x8282;&#x7B49;&#x4E8E;8&#x4F4D;&#x4E8C;&#x8FDB;&#x5236;&#x6570;</li>
<li>&#x4E00;&#x4E2A;&#x4F4D;&#x5C31;&#x4EE3;&#x8868;&#x4E00;&#x4E2A;0&#x6216;1&#xFF0C;&#x6BCF;8&#x4E2A;&#x4F4D;&#xFF08;bit&#xFF09;&#x7EC4;&#x6210;&#x4E00;&#x4E2A;&#x5B57;&#x8282;&#xFF08;Byte&#xFF09;</li>
<li>&#x5B57;&#x8282;&#x662F;&#x901A;&#x8FC7;&#x7F51;&#x7EDC;&#x4F20;&#x8F93;&#x4FE1;&#x606F;&#x7684;&#x5355;&#x4F4D;</li>
<li>&#x4E00;&#x4E2A;&#x5B57;&#x8282;&#x6700;&#x5927;&#x503C;&#x5341;&#x8FDB;&#x5236;&#x6570;&#x662F;255 <code>2**8-1;</code></li>
</ul>
<h2 id="t2&#x8FDB;&#x5236;">&#x8FDB;&#x5236; <a href="#t2&#x8FDB;&#x5236;"> # </a></h2>
<ul>
<li>0b 2&#x8FDB;&#x5236;</li>
<li>0x 16&#x8FDB;&#x5236;</li>
<li><p>0o 8&#x8FDB;&#x5236;</p>
</li>
<li><p>&#x5C06;&#x4EFB;&#x610F;&#x8FDB;&#x5236;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x4E3A;&#x5341;&#x8FDB;&#x5236;</p>
<ul>
<li>parseInt(&quot;11&quot;, 2); // 3 2&#x8FDB;&#x5236;&#x8F6C;10&#x8FDB;&#x5236;</li>
<li>parseInt(&quot;77&quot;, 8); // 63 8&#x8FDB;&#x5236;&#x8F6C;10&#x8FDB;&#x5236;</li>
<li>parseInt(&quot;e7&quot;, 16); //175 16&#x8FDB;&#x5236;&#x8F6C;10&#x8FDB;&#x5236;</li>
</ul>
</li>
<li>&#x5C06;10&#x8FDB;&#x5236;&#x8F6C;&#x6362;&#x4E3A;&#x5176;&#x5B83;&#x8FDB;&#x5236;&#x5B57;&#x7B26;&#x4E32;<ul>
<li>(3).toString(2)) // &quot;11&quot; &#x5341;&#x8FDB;&#x5236;&#x8F6C;2&#x8FDB;&#x5236;</li>
<li>(17).toString(16) // &quot;11&quot; &#x5341;&#x8FDB;&#x5236;&#x8F6C;16&#x8FDB;&#x5236;</li>
<li>(33).toString(32) // &quot;11&quot; &#x5341;&#x63D0;&#x5236;&#x8F6C;32&#x8FDB;&#x5236;</li>
</ul>
</li>
</ul>
<h2 id="t33. &#x5B9A;&#x4E49;buffer&#x7684;&#x4E09;&#x79CD;&#x65B9;&#x5F0F;">3. &#x5B9A;&#x4E49;buffer&#x7684;&#x4E09;&#x79CD;&#x65B9;&#x5F0F; <a href="#t33. &#x5B9A;&#x4E49;buffer&#x7684;&#x4E09;&#x79CD;&#x65B9;&#x5F0F;"> # </a></h2>
<h3 id="t43.1 &#x901A;&#x8FC7;&#x957F;&#x5EA6;&#x5B9A;&#x4E49;buffer">3.1 &#x901A;&#x8FC7;&#x957F;&#x5EA6;&#x5B9A;&#x4E49;buffer <a href="#t43.1 &#x901A;&#x8FC7;&#x957F;&#x5EA6;&#x5B9A;&#x4E49;buffer"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x957F;&#x5EA6;&#x4E3A; 10&#x3001;&#x4E14;&#x7528; 0 &#x586B;&#x5145;&#x7684; Buffer&#x3002;</span>
<span class="hljs-keyword">const</span> buf1 = Buffer.alloc(<span class="hljs-number">10</span>);
<span class="hljs-comment">// &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x957F;&#x5EA6;&#x4E3A; 10&#x3001;&#x4E14;&#x7528; 0x1 &#x586B;&#x5145;&#x7684; Buffer&#x3002;</span>
<span class="hljs-keyword">const</span> buf2 = Buffer.alloc(<span class="hljs-number">10</span>, <span class="hljs-number">1</span>);
<span class="hljs-comment">// &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x957F;&#x5EA6;&#x4E3A; 10&#x3001;&#x4E14;&#x672A;&#x521D;&#x59CB;&#x5316;&#x7684; Buffer&#x3002;</span>
<span class="hljs-keyword">const</span> buf3 = Buffer.allocUnsafe(<span class="hljs-number">10</span>);
</code></pre>
<h3 id="t53.2 &#x901A;&#x8FC7;&#x6570;&#x7EC4;&#x5B9A;&#x4E49;buffer">3.2 &#x901A;&#x8FC7;&#x6570;&#x7EC4;&#x5B9A;&#x4E49;buffer <a href="#t53.2 &#x901A;&#x8FC7;&#x6570;&#x7EC4;&#x5B9A;&#x4E49;buffer"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5305;&#x542B; [0x1, 0x2, 0x3] &#x7684; Buffer&#x3002;</span>
<span class="hljs-keyword">const</span> buf4 = Buffer.from([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]);
</code></pre>
<blockquote>
<p>&#x6B63;&#x5E38;&#x60C5;&#x51B5;&#x4E0B;&#x4E3A;0-255&#x4E4B;&#x95F4;;</p>
</blockquote>
<h3 id="t63.3 &#x5B57;&#x7B26;&#x4E32;&#x521B;&#x5EFA;">3.3 &#x5B57;&#x7B26;&#x4E32;&#x521B;&#x5EFA; <a href="#t63.3 &#x5B57;&#x7B26;&#x4E32;&#x521B;&#x5EFA;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> buf5 = Buffer.from(<span class="hljs-string">&apos;&#x73E0;&#x5CF0;&#x57F9;&#x8BAD;&apos;</span>);
</code></pre>
<h2 id="t74.buffer&#x5E38;&#x7528;&#x65B9;&#x6CD5;">4.buffer&#x5E38;&#x7528;&#x65B9;&#x6CD5; <a href="#t74.buffer&#x5E38;&#x7528;&#x65B9;&#x6CD5;"> # </a></h2>
<h3 id="t84.1 buf.fill(value[, offset[, end]][, encoding])">4.1 buf.fill(value[, offset[, end]][, encoding]) <a href="#t84.1 buf.fill(value[, offset[, end]][, encoding])"> # </a></h3>
<p>&#x624B;&#x52A8;&#x521D;&#x59CB;&#x5316;,&#x64E6;&#x5E72;&#x51C0;&#x684C;&#x5B50;,&#x5C06;buffer&#x5185;&#x5BB9;&#x6E05;0</p>
<pre><code class="lang-javascript">buffer.fill(<span class="hljs-number">0</span>);
</code></pre>
<h3 id="t94.2 write&#x65B9;&#x6CD5;">4.2 write&#x65B9;&#x6CD5; <a href="#t94.2 write&#x65B9;&#x6CD5;"> # </a></h3>
<p>buf.write(string[, offset[, length]][, encoding])</p>
<pre><code class="lang-javascript">buffer.write(<span class="hljs-string">&apos;&#x73E0;&apos;</span>,<span class="hljs-number">0</span>,<span class="hljs-number">3</span>,<span class="hljs-string">&apos;utf8&apos;</span>);
buffer.write(<span class="hljs-string">&apos;&#x5CF0;&apos;</span>,<span class="hljs-number">3</span>,<span class="hljs-number">3</span>,<span class="hljs-string">&apos;utf8&apos;</span>); <span class="hljs-comment">//&#x73E0;&#x5CF0;</span>
</code></pre>
<h3 id="t104.3 writeInt8">4.3 writeInt8 <a href="#t104.3 writeInt8"> # </a></h3>
<pre><code>var buf = new Buffer(4);
buf.writeInt8(0,0);
buf.writeInt8(16,1);
buf.writeInt8(32,2);
buf.writeInt8(48,3);//16*3*/
console.log(buf);
console.log(buf.readInt8(0));
console.log(buf.readInt8(1));
console.log(buf.readInt8(2));
console.log(buf.readInt8(3));
</code></pre><h4 id="t114.3.1 Little-Endian&amp;Big-Endian">4.3.1 Little-Endian&amp;Big-Endian <a href="#t114.3.1 Little-Endian&amp;Big-Endian"> # </a></h4>
<p>&#x4E0D;&#x540C;&#x7684;CPU&#x6709;&#x4E0D;&#x540C;&#x7684;&#x5B57;&#x8282;&#x5E8F;&#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x4E9B;&#x5B57;&#x8282;&#x5E8F;&#x662F;&#x6307;&#x6574;&#x6570;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x4FDD;&#x5B58;&#x7684;&#x987A;&#x5E8F;&#x3002;</p>
<ul>
<li>Big-endian&#xFF1A;&#x5C06;&#x9AD8;&#x5E8F;&#x5B57;&#x8282;&#x5B58;&#x50A8;&#x5728;&#x8D77;&#x59CB;&#x5730;&#x5740;&#xFF08;&#x9AD8;&#x4F4D;&#x7F16;&#x5740;&#xFF09;</li>
<li>Little-endian&#xFF1A;&#x5C06;&#x4F4E;&#x5E8F;&#x5B57;&#x8282;&#x5B58;&#x50A8;&#x5728;&#x8D77;&#x59CB;&#x5730;&#x5740;&#xFF08;&#x4F4E;&#x4F4D;&#x7F16;&#x5740;&#xFF09;</li>
</ul>
<pre><code>let buf3 = new Buffer(4);
buf3.writeInt16BE(2**8,0);
console.log(buf3);//&lt;Buffer 01 00 00 00&gt;
console.log(buf3.readInt16BE(0));

buf3.writeInt16LE(2**8,2);
console.log(buf3);//&lt;Buffer 01 00 00 01&gt;
console.log(buf3.readInt16LE(2));
</code></pre><h3 id="t124.3 toString&#x65B9;&#x6CD5;">4.3 toString&#x65B9;&#x6CD5; <a href="#t124.3 toString&#x65B9;&#x6CD5;"> # </a></h3>
<p>buf.toString([encoding[, start[, end]]])</p>
<pre><code class="lang-javascript">buffer.toString(<span class="hljs-string">&apos;utf8&apos;</span>,<span class="hljs-number">3</span>,<span class="hljs-number">6</span>)
</code></pre>
<h3 id="t134.4 slice&#x65B9;&#x6CD5;">4.4 slice&#x65B9;&#x6CD5; <a href="#t134.4 slice&#x65B9;&#x6CD5;"> # </a></h3>
<p>buf.slice([start[, end]])</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> newBuf = buffer.slice(<span class="hljs-number">0</span>,<span class="hljs-number">4</span>);
</code></pre>
<h4 id="t144.4.1 &#x622A;&#x53D6;&#x4E71;&#x7801;&#x95EE;&#x9898;">4.4.1 &#x622A;&#x53D6;&#x4E71;&#x7801;&#x95EE;&#x9898; <a href="#t144.4.1 &#x622A;&#x53D6;&#x4E71;&#x7801;&#x95EE;&#x9898;"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> {StringDecoder}  = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;string_decoder&apos;</span>);
<span class="hljs-keyword">let</span> sd = <span class="hljs-keyword">new</span> StringDecoder();
<span class="hljs-keyword">let</span> buffer = <span class="hljs-keyword">new</span> Buffer(<span class="hljs-string">&apos;&#x73E0;&#x5CF0;&apos;</span>);
<span class="hljs-built_in">console</span>.log(sd.write(buffer.slice(<span class="hljs-number">0</span>,<span class="hljs-number">4</span>)));
<span class="hljs-built_in">console</span>.log(sd.write(buffer.slice(<span class="hljs-number">4</span>)));
</code></pre>
<h3 id="t154.5 copy&#x65B9;&#x6CD5;">4.5 copy&#x65B9;&#x6CD5; <a href="#t154.5 copy&#x65B9;&#x6CD5;"> # </a></h3>
<p>&#x590D;&#x5236;Buffer &#x628A;&#x591A;&#x4E2A;buffer&#x62F7;&#x8D1D;&#x5230;&#x4E00;&#x4E2A;&#x5927;buffer&#x4E0A;</p>
<pre><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])
</code></pre><pre><code class="lang-javascript"><span class="hljs-keyword">let</span> buf5 = Buffer.from(<span class="hljs-string">&apos;&#x73E0;&#x5CF0;&#x57F9;&#x8BAD;&apos;</span>);
<span class="hljs-keyword">let</span> buf6 = Buffer.alloc(<span class="hljs-number">6</span>);
buf5.copy(buf6,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">4</span>);
buf5.copy(buf6,<span class="hljs-number">3</span>,<span class="hljs-number">3</span>,<span class="hljs-number">6</span>);
<span class="hljs-comment">//buf6=&#x73E0;&#x5CF0;</span>
</code></pre>
<pre><code>Buffer.prototype.copy2 = function(targetBuffer,targetStart,sourceStart,sourceEnd){
    for(let i=sourceStart;i&lt;sourceEnd;i++){
        target[targetStart+i] = this[i];
    }
}
</code></pre><h3 id="t164.6 concat&#x65B9;&#x6CD5;">4.6 concat&#x65B9;&#x6CD5; <a href="#t164.6 concat&#x65B9;&#x6CD5;"> # </a></h3>
<pre><code class="lang-javascript">Buffer.concat(list[, totalLength])
</code></pre>
<pre><code>let buf1 = Buffer.from(&apos;&#x73E0;&apos;);
let buf2 = Buffer.from(&apos;&#x5CF0;&apos;);
let buf3 = Buffer.concat([buf1,buf2],3);
console.log(buf3.toString());
</code></pre><pre><code>Buffer.concat2 = function (list = [], total = list.reduce((len, item) =&gt; len + item.length, 0)) {
    if (list.length == 1)
        return list[0];
    let result = Buffer.alloc(total);
    let pos = 0;
    for (let bf of list) {
        for (let b of bf) {
            if (pos &lt; total)
                result[pos++] = b;
            else
                return result;
        }
    }
    return result;
}
</code></pre><pre><code>Buffer.myConcat = function(list,totalLength=list.reduce((len,item)=&gt;len+item.length,0)){
  if(list.length==0)
    return list[0];
  let newBuffer = Buffer.alloc(totalLength);
  let offset = 0;
  for(let i=0;i&lt;list.length;i++){
    if(offset+list[i].length&gt;totalLength){
      list[i].copy(newBuffer,offset,0,totalLength-offset);
      break;
    }else{
      list[i].copy(newBuffer,offset,0,list[i].length);
      offset+=list[i].length;
    }
  }
  return newBuffer;
}
</code></pre><h3 id="t174.7 isBuffer">4.7 isBuffer <a href="#t174.7 isBuffer"> # </a></h3>
<p>&#x5224;&#x65AD;&#x662F;&#x5426;&#x662F;buffer</p>
<pre><code class="lang-javascript">Buffer.isBuffer
</code></pre>
<h3 id="t184.8 length">4.8 length <a href="#t184.8 length"> # </a></h3>
<p>&#x83B7;&#x53D6;&#x5B57;&#x8282;&#x957F;&#x5EA6;(&#x663E;&#x793A;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x6240;&#x4EE3;&#x8868;buffer&#x7684;&#x957F;&#x5EA6;)</p>
<pre><code class="lang-javascript">Buffer.byteLength(<span class="hljs-string">&quot;&#x73E0;&#x5CF0;&quot;</span>);
buffer.length;
</code></pre>
<h2 id="t196. base64">6. base64 <a href="#t196. base64"> # </a></h2>
<ul>
<li>Base64&#x662F;&#x7F51;&#x7EDC;&#x4E0A;&#x6700;&#x5E38;&#x89C1;&#x7684;&#x7528;&#x4E8E;&#x4F20;&#x8F93;8Bit&#x5B57;&#x8282;&#x7801;&#x7684;&#x7F16;&#x7801;&#x65B9;&#x5F0F;&#x4E4B;&#x4E00;&#xFF0C;Base64&#x5C31;&#x662F;&#x4E00;&#x79CD;&#x57FA;&#x4E8E;64&#x4E2A;&#x53EF;&#x6253;&#x5370;&#x5B57;&#x7B26;&#x6765;&#x8868;&#x793A;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#x7684;&#x65B9;&#x6CD5;&#x3002;</li>
<li>Base64&#x8981;&#x6C42;&#x628A;&#x6BCF;&#x4E09;&#x4E2A;8Bit&#x7684;&#x5B57;&#x8282;&#x8F6C;&#x6362;&#x4E3A;&#x56DB;&#x4E2A;6Bit&#x7684;&#x5B57;&#x8282;&#xFF08;3<em>8 = 4</em>6 = 24&#xFF09;&#xFF0C;&#x7136;&#x540E;&#x628A;6Bit&#x518D;&#x6DFB;&#x4E24;&#x4F4D;&#x9AD8;&#x4F4D;0&#xFF0C;&#x7EC4;&#x6210;&#x56DB;&#x4E2A;8Bit&#x7684;&#x5B57;&#x8282;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x8F6C;&#x6362;&#x540E;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x7406;&#x8BBA;&#x4E0A;&#x5C06;&#x8981;&#x6BD4;&#x539F;&#x6765;&#x7684;&#x957F;1/3</li>
</ul>
<pre><code>const CHARTS = &apos;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/&apos;;
function transfer(str){
  let buf = Buffer.from(str);
  let result = &apos;&apos;;
  for(let b of buf){
      result += b.toString(2);
  }
    return result.match(/(\d{6})/g).map(val=&gt;parseInt(val,2)).map(val=&gt;CHARTS[val]).join(&apos;&apos;);
}
let r = transfer(&apos;&#x73E0;&apos;);//54+g
</code></pre>
        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
